

setInterval(() => {
    location.reload();
}, 600000);


//https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url=/product/1185794392/?layout_container=pdpPage2column&layout_page_index=2
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    }
    return 0;
 }
 console.log("shein stock")


$(document).ready(function() {
    getStockByFetch();
});
function  getStockByFetch(msg){

    if(location.href.includes("www.shein.com.hk/risk/action/limit")){
        setTimeout(function() {
            window.close();
        }, 5000);
        return;
    }else{
        setTimeout(function() {
            window.close();
        }, 20000);
    }
    var bodyContent = $('body').text();
    console.log(bodyContent)

    var jsonObject = JSON.parse(bodyContent);

    if(jsonObject['code']=="200301"){
        var type = "sheindetaildel";
        var data={}
        data.id = getQueryString("bid");
        console.log(data);
        chrome.runtime.sendMessage({ data: {type,data }, close: false })
        setTimeout(function() {
            window.close();
        }, 10000);
    }else if(jsonObject['info']['saleAttr'] && jsonObject['info']['saleAttr']['multiLevelSaleAttribute'] && jsonObject['info']['saleAttr']['multiLevelSaleAttribute']['sku_list']){
        var sku_list = jsonObject['info']['saleAttr']['multiLevelSaleAttribute']['sku_list'];
        console.log(sku_list);

        var stock_map = {};
        var index = 0;
        var price = jsonObject['info']['priceInfo']['salePrice']['amount'];
        sku_list.forEach((item) => {
            if(item['sku_sale_attr'] && item['sku_sale_attr'].length>0){
                stock_map[item['sku_sale_attr'][0]['attr_value_name']] = item['stock'];
            }else{
                stock_map[index] = item['stock'];
            }
            index++;

        })

        var type = "sheinstock_s1";
        var id = getQueryString("bid");
        console.log('sheinstock send.');
        chrome.runtime.sendMessage({ data: {type,id, stock_map,price}, close: false });

        setTimeout(function() {

            window.close();
        }, 10000);
    }else if(jsonObject['info']['productInfo']['stock']==0){

        var type = "sheinstock_s1";
        var id = getQueryString("bid");
        console.log('sheinstock send no stock.');

        var stock_map = {};
        stock_map[0] = 0;
        var price = jsonObject['info']['priceInfo']['salePrice']?jsonObject['info']['priceInfo']['salePrice']['amount']:0;
        chrome.runtime.sendMessage({ data: {type,id, stock_map,price}, close: false });
        setTimeout(function() {
            window.close();
        }, 10000);
    }


    console.log(stock_map);

}

